package cn.shengchao.examstar.gankao.http;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.crypto.digest.DigestUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @program: examstarapi
 * @description: 请求参数封装
 * @author: Xiong weiqiang
 * @create: 2025-06-23 17:16
 */
public class GanKaoHttpParams {
    public static final String APP_ID="yunshi";
    public static final String APP_SECRET="e4cd808c-4a79-11f0-bca4-b8599fe54952";
    public static final String TEST_URL="https://www.gankao.com/api/service";
    public static final String VIDEO_URL="http://lubo.gankao.com/tag/";
    public static final String URI_CREATE_USER_API="/createUser";
    public static final String URI_GET_SEARCH_CRITERIA_API ="/getSearchCriteria";
    public static final String URI_SHOW_COURSES_API ="/showCourses";
    public static final String URI_GET_COURSE_DETAIL_API ="/getCourseDetail";
    public static final String URI_BUY_SECTION_API ="/buySection";
    public static final String URI_GET_USER_SECTIONS_API ="/getUserSections";
    public static final String URI_GET_PLAY_VIDEO_RESOURCE_FOR_WEB_API ="/getPlayVideoResourceForWeb";
    public static final String URI_GET_SPECIAL_TAG_COURSES_API ="/getSepcialTagCourses";

//    public static final String URI_CREATE_USER_API="/buySection";
    public static String getSecretTokenByParams(Map<String,Object> params, String secret) {
        StringBuilder paramsValue=new StringBuilder();
        if (params != null &&!params.isEmpty()) {
            List<String> keys = new ArrayList(params.keySet());
            List<String> sortKeys= CollUtil.sort(keys, String::compareTo);
            for (String key : sortKeys) {
                paramsValue.append(params.getOrDefault(key,"").toString());
            }
            paramsValue.append(secret);
            return DigestUtil.md5Hex(paramsValue.toString());
        }
        return null;
    }
    /**
     * unicode转为中中文
     * @param unicodeEscapedString
     */
    public static String unicodeEscapeToChinese (String unicodeEscapedString) {
        // 解码Unicode转义序列
        StringBuilder decodedString = new StringBuilder();
        int i = 0;
        while (i < unicodeEscapedString.length()) {
            if (unicodeEscapedString.startsWith("\\u", i)) {
                // 找到一个Unicode转义序列
                int codePoint = Integer.parseInt(unicodeEscapedString.substring(i + 2, i + 6), 16);
                decodedString.appendCodePoint(codePoint);
                i += 6;
                // 跳过这个Unicode转义序列
            } else {
                // 不是Unicode转义序列，直接添加字符
                decodedString.append(unicodeEscapedString.charAt(i));
                i++;
            }
        }
        // 打印解码后的字符串
        return decodedString.toString();
    }
}